package linuxbox.poll.servlet;

import javax.servlet.http.*;
import org.apache.log4j.Logger;

import linuxbox.common.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class BoxPollSettingsAction extends Action
{
  protected static Logger logger = Logger.getLogger(BoxPollSettingsAction.class.getName());
  protected static String editJSP = null;
  protected static String viewJSP = null;

  public void doAction(HttpSession aSession, BoxSession aBoxSession,
                       HttpServletRequest req, HttpServletResponse resp) throws Exception
  {
    String str = req.getParameter("SubmitCancel");
    if ( str != null )
    {
      sendFile(viewJSP,req,resp);
      return;
    }

    str = (String) req.getParameter("SubmitEdit");
    if ( str != null )
    {
      GetSettings(req, aBoxSession);
      sendFile(editJSP,req,resp);
      return;
    }
    str = (String) req.getParameter("SubmitSave");
    if ( str != null )
    {
      SaveSettings(req, aBoxSession);
      sendFile(viewJSP,req,resp);
      return;
    }
    sendFile(viewJSP,req,resp);
  }

  protected void SaveSettings(HttpServletRequest req, BoxSession session)
  {
    try
    {
      String str = (String) req.getParameter("VIEW_TYPE");
      session.setConfigString("poll", "General", "Default View", str);

      str = (String) req.getParameter("ENTRIES_PER_PAGE");
      session.setConfigInt("poll", "General", "Entries Per Page", Integer.parseInt(str));
    }
    catch (Exception e)
    {
      //TODOXX: if input data error, should forward to the error page
      if (logger.isDebugEnabled())
      {
        logger.error(e);
      }
    }
  }

  protected void GetSettings(HttpServletRequest req, BoxSession session)
  {
    try
    {
      String str = session.getConfigString("poll", "General", "Default View");
      req.setAttribute("DEFAULT_VIEW", str);

      int i = session.getConfigInt("poll", "General", "Entries Per Page");
      req.setAttribute("ENTRIES_PER_PAGE", String.valueOf(i));
    }
    catch (Exception e)
    {
      if (logger.isDebugEnabled())
      {
        logger.error(e);
      }
    }
  }

  public void setEditJSP(String str)
  {
    editJSP = str;
  }
  public void setViewJSP(String str)
  {
    viewJSP = str;
  }
}
